home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / cool / ge_cool.lha / GE_COOL2.1 / src / Handle / Shared.h < prev   
C/C++ Source or Header  |  1992-05-16  |  2KB  |  88 lines

  1. //
  2. // Copyright (C) 1992 General Electric Company.
  3. //
  4. // Permission is granted to any individual or institution to use, copy, modify,
  5. // and distribute this software, provided that this complete copyright and
  6. // permission notice is maintained, intact, in all copies and supporting
  7. // documentation.
  8. //
  9. // General Electric Company provides this software "as is" without
  10. // express or implied warranty.
  11. //
  12. // Created: VDN 03/25/92 -- Initial design
  13. //
  14. // Shared is a mixin slot, which store the number of references and 
  15. // handles pointed at an object. Shared does not have a virtual destructor,
  16. // and so the deletion of the object must be done by the caller of dereference.
  17.  
  18. #ifndef SHAREDH
  19. #define SHAREDH
  20.  
  21. #ifndef MISCH
  22. #include <misc.h>                // for INVALID marker
  23. #endif
  24.  
  25. class CoolShared {
  26. public:
  27.   inline CoolShared();                // Initialize ref_count=0
  28.   inline ~CoolShared();                // Destructor
  29.  
  30.   inline int reference_count();            // query current ref_count
  31.   inline int reference();            // inc ref_count
  32.   inline int dereference();            // dec ref_count
  33.   
  34.   static inline int reference(CoolShared* ptr);    // noop if ptr=NULL
  35.   static inline int dereference(CoolShared* ptr);
  36.  
  37. private:
  38.   int ref_count;                // count of active references
  39. };
  40.  
  41. // CoolShared() -- Default constructor initializes ref_count to 0
  42.  
  43. inline CoolShared::CoolShared()
  44. : ref_count(0) {}                // ref_count initially 0
  45.  
  46. // ~CoolShared -- Destructor is not virtual, so caller of dereference
  47. //               must call delete on object ptr, with correct type.
  48.  
  49. inline CoolShared::~CoolShared() {}        // nothing
  50.  
  51. // reference_count() -- Query current ref_count
  52.  
  53. inline int CoolShared::reference_count() {
  54.   return ref_count;
  55. }
  56.  
  57. // reference() -- increment ref_count and return new count.
  58.  
  59. inline int CoolShared::reference() {
  60.   return ++ref_count;
  61. }
  62.  
  63. // dereference() -- decrement ref_count and return new count.
  64.  
  65. inline int CoolShared::dereference(){
  66.   return --ref_count;
  67. }
  68.  
  69. // Check pointers first, noop if ptr == NULL.
  70.   
  71. static inline int CoolShared::reference (CoolShared* ptr) {
  72.   if (ptr)
  73.     return ptr->reference();
  74.   else
  75.     return INVALID;
  76. }
  77.  
  78. static inline int CoolShared::dereference (CoolShared* ptr) {
  79.   if (ptr)
  80.     return ptr->dereference();
  81.   else
  82.     return INVALID;
  83. }
  84.  
  85.  
  86.  
  87. #endif                        // SHAREDH
  88.